#include <cstdlib>
#include <iostream>

using namespace std;

void swap(int* list, int m, int n)
{
    if(m == n) return;
    
    int temp = list[ m ];
    list[ m ] = list[ n ];
    list[ n ] = temp;
}


void printArrange(int* list, int start , int end)
{  
    if(start == end)
    { 
        for(int i = 0; i <= end; i++)  
            cout << list[i] << "  ";
        
        cout << endl;
    }
    else  
        for(int i = start; i <= end; i++)
        {  
            swap(list, i, start);  
            printArrange(list, start+1, end);  
            
            //swap it back for next iteration; silly but saving space.
            swap(list, i, start);   
        }  
}  

int main(int argc, char *argv[])
{
    const int COUNT = 5;
    int list[COUNT] = {1, 2, 3, 4, 5};
    
    //initial start index must be 0
    printArrange(list, 0, COUNT - 1);  
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
